import sys
sys.path.append('../gen-py')

from com.demo.thrift import HelloWorldService
from com.demo.thrift.ttypes import *
from com.demo.thrift.constants import *

from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol


if __name__ == '__main__':
    transport = TSocket.TSocket('localhost', 7099)
    # Buffering is critical. Raw sockets are very slow
    transport = TTransport.TBufferedTransport(transport)

    # Wrap in a protocol
    protocol = TBinaryProtocol.TBinaryProtocol(transport)

    # Create a client to use the protocol encoder
    client = HelloWorldService.Client(protocol)

    # Connect!
    transport.open()

    try:
        username = 'py'
        result = client.sayHello(username)
        print('Thrift client result = ', result)
    except InvalidOperation as e:
        print('InvalidOperation: %r' % e)
    
    # Close!
    transport.close()
